package com.sirui.cloud.oc.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sirui.cloud.oc.dto.OrderDto;
import com.sirui.cloud.oc.dto.PropertyB;
import com.sirui.cloud.oc.entity.OrderInfoEntity;
import com.sirui.cloud.oc.openFeign.AccountPayFeign;
import com.sirui.cloud.oc.openFeign.AccountPayFeignSimple;
import com.sirui.cloud.oc.service.OrderInfoService;


@RestController
@RequestMapping("order")
public class OrderController {
	
	private static final Logger log = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	private OrderInfoService orderInfoSvc;

	@RequestMapping("")
	public OrderDto order(@Validated OrderDto dto) {
		log.info("test1....");
		orderInfoSvc.test1();
		log.info("test2....{}。。。", dto.getUserId());
		return dto;
	}

//	@Autowired
	private PropertyB pb;

	@RequestMapping("test1")
//	@LogArround
	public String test1() {
		log.info("test1....");
		return "simple call" + feignSimple.pay();
	}

	@RequestMapping("save")
	public String saveOrder() {

		OrderInfoEntity entity = new OrderInfoEntity();
		entity.setUserId("user11");
		entity.setCount(100);
		orderInfoSvc.save(entity);
		return "ok";
	}

	@Autowired
	private AccountPayFeign payFeign;
	@Autowired
	private AccountPayFeignSimple feignSimple;

	@RequestMapping("pay")
//	@LogArround
	public String pay() {
		log.info("pay....");
		return "order call: " + payFeign.pay();
	}

	@RequestMapping("create")
//	@LogArround
	@Transactional
	public String create(String userId, String commodityCode, int orderCount) {
		int money = orderCount * 50;
		int debit = payFeign.debit(userId, money);

		OrderInfoEntity order = new OrderInfoEntity();
		order.setUserId(userId);
		order.setCommodityCode(commodityCode);
		order.setCount(orderCount);
		order.setMoney(money);

		// INSERT INTO orders ...
		boolean flag = orderInfoSvc.save(order);
		return String.valueOf(flag);
	}
}
